package me.firstapp.service.bbs.impl;

import java.math.BigDecimal;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.utils.MD5Utils;
import me.firstapp.common.utils.StrUtils;
import me.firstapp.module.bbs.MemberPointAccountTrace;
import me.firstapp.module.bbs.PointAccount;
import me.firstapp.module.shop.PointRechargeCard;
import me.firstapp.repository.bbs.MemberPointAccountTraceRepository;
import me.firstapp.repository.bbs.PointAccountRepository;
import me.firstapp.repository.shop.PointRechargeCardRepository;
import me.firstapp.service.bbs.PointAccountService;

@Service
public class PointAccountServiceImpl implements PointAccountService {

	@Autowired
	private PointAccountRepository pointAccountRepository;

	@Autowired
	private PointRechargeCardRepository pointRechargeCardRepository;

	@Autowired
	private MemberPointAccountTraceRepository memberPointAccountTraceRepository;

	@Override
	public PointAccount findByMemberId(Long memberId, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		return pointAccountRepository.findByMemberId(memberId, apiClientId);
	}

	@Override
	public PointAccount updatePayPassword(Long memberId, String oldPassword, String newPassword, Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		PointAccount pointAccount = pointAccountRepository.findByMemberId(memberId, apiClientId);
		if (pointAccount == null) {
			throw new ServiceException(StatusHouse.POINT_ACCOUNT_NOT_EXIST);
		}
		if (pointAccount.getHasPassword() == 1) {
			if (StrUtils.isNULL(oldPassword)) {
				throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "旧支付密码");
			}
		}
		if (StrUtils.isNULL(newPassword)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "新支付密码");
		}
		if (pointAccount.getHasPassword() == 1) {
			if (MD5Utils.verify(oldPassword, pointAccount.getPassword())) {
				throw new ServiceException(StatusHouse.OLD_PAY_PASSWORD_IS_WRONG);
			}
		}
		if (pointAccount.getHasPassword() == 0) {
			pointAccount.setHasPassword(1);
		}

		pointAccount.setPassword(MD5Utils.generate(newPassword));
		pointAccountRepository.update(pointAccount);

		return pointAccount;
	}

	@Override
	public PointAccount executeRecharge(Long memberId, String cardNo, String password, String payPassword,
			Long apiClientId) {
		if (memberId == null) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "用户id");
		}
		if (StrUtils.isNULL(cardNo)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "卡号");
		}
		if (StrUtils.isNULL(password)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "卡密");
		}
		if (StrUtils.isNULL(payPassword)) {
			throw new ServiceException(StatusHouse.COMMON_STATUS_PARAM_REQUIRED_ERROR, "支付密码");
		}
		PointAccount pointAccount = pointAccountRepository.findByMemberId(memberId, apiClientId);
		if (pointAccount == null) {
			throw new ServiceException(StatusHouse.POINT_ACCOUNT_NOT_EXIST);
		}
		PointRechargeCard pointRechargeCard = pointRechargeCardRepository.findByCardNo(cardNo);
		if (pointRechargeCard == null) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_NOT_EXIST);
		}
		// 判断有效期
		Date now = new Date();
		if (pointRechargeCard.getEndDate().getTime() < now.getTime()
				|| pointRechargeCard.getStartDate().getTime() > now.getTime()) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_IS_END_DATE);
		}
		// 判断状态
		if (pointRechargeCard.getStatus() != 1) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_IS_INVAILED);
		}
		if (pointRechargeCard.getUseStatus() == 0 || pointRechargeCard.getUseStatus() == 3) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_IS_INVAILED);
		}
		// 验证支付密码
		if (!MD5Utils.verify(payPassword, pointAccount.getPassword())) {
			throw new ServiceException(StatusHouse.PAY_PASSWORD_IS_WRONG);
		}
		// 验证卡密
		if (!password.equals(pointRechargeCard.getPassword())) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_KEY_IS_WRONG);
		}
		// 校验卡积分值是否被修改过
		if (!MD5Utils.verify(pointRechargeCard.getPointValue().toString(), pointRechargeCard.getSalt())) {
			throw new ServiceException(StatusHouse.POINT_RECHARGE_CARD_KEY_IS_WRONG);
		}
		// 进行充值
		Double beforeValue = pointAccount.getCurrentValue();
		BigDecimal bBeforeValue = new BigDecimal(beforeValue);
		Double cardPointValue = pointRechargeCard.getPointValue();
		BigDecimal bCardPointValue = new BigDecimal(cardPointValue);
		Double afterValue = bBeforeValue.add(bCardPointValue).doubleValue();
		pointAccount.setCurrentValue(afterValue);
		pointAccount.setSalt(MD5Utils.generate(afterValue.toString()));
		pointAccountRepository.update(pointAccount);

		// 更改卡状态
		pointRechargeCard.setUseStatus(3);
		pointRechargeCard.setRechargeTime(new Date());
		pointRechargeCardRepository.update(pointRechargeCard);

		// 积分变动记录
		MemberPointAccountTrace trace = new MemberPointAccountTrace();
		trace.setPointAccountId(pointAccount.getId());
		trace.setBeforeValue(beforeValue);
		trace.setAfterValue(afterValue);
		trace.setChangeValue(pointRechargeCard.getPointValue());
		trace.setChangeTime(new Date());
		trace.setRemark("积分充值");
		trace.setApiClientId(apiClientId);
		memberPointAccountTraceRepository.save(trace);

		return pointAccount;
	}

}
